package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;


    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 根据id查询菜品状态.
     *
     * @param id
     */
    public Dish getById(Long id) {
        Dish dish = dishMapper.getById(id);
        return dish;
    }


    /**
     * 根据ids查询有无关联的套餐.
     *
     * @param ids
     */
    @Override
    public void getSetmealwithDish(List<Long> ids) {
//
        for (Long id : ids) {
            SetmealDish setmealAtDish = setmealMapper.getSetmealAtDish(id);
            if (setmealAtDish != null) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
            }
        }

    }


    /**
     * 删除菜品对应的口味数据.
     *
     * @param ids
     */
    @Override
    public void deleteFlavor(List<Long> ids) {
//        删除口味
        for (Long id : ids) {
            dishFlavorMapper.deleteDishByFlavor(id);
        }

//删除菜品.
        for (Long id : ids) {
            dishMapper.deleteById(id);
        }
    }

    /**
     * 根据id查询菜品和口味.
     *
     * @param id
     * @return
     */
    @Override
    public DishVO getDishWithFlavor(long id) {
//        根据id查询菜品数据
        Dish byId = dishMapper.getById(id);
//根据id查询菜品口味数据.
        List<DishFlavor> Flavors = dishFlavorMapper.getByDishIdFlavor(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(byId, dishVO);
        dishVO.setFlavors(Flavors);
        return dishVO;
    }

    /**
     * 根据id更新菜品数据.
     *
     * @param dishDTO
     */
    @Override
    @Transactional
    public void updateWithFlavor(DishDTO dishDTO) {
//        更新菜品数据
        dishMapper.updateDish(dishDTO);

        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null) {
            //        删除菜品口味数据,之后再重新添加.
            dishFlavorMapper.deleteDishByFlavor(dishDTO.getId());
//        重新添加菜品口味数据.
            dishFlavorMapper.saveFlavor(flavors);
        }
    }


    /**
     * 新增菜品对应口味
     * 添加菜品后返回主键之后保存口味.
     *
     * @param dishDTO
     */
    @Override
    @Transactional
    public void saveWithFlavor(DishDTO dishDTO) {
//向菜品表插入一条数据.
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.insert(dish);

//向口味表插入多个数据.

        Long id = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
//            说明有口味数据.
            flavors.stream().forEach(item -> {
                item.setDishId(id);

            });

            dishFlavorMapper.saveFlavor(flavors);
        }
    }

    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        //下一条sql进行分页，自动加入limit关键字分页
        Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

//    @Autowired
//    private SetmealDishMapper setmealDishMapper;


    /**
     * 条件查询菜品和口味
     *
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {




        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d, dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishIdFlavor(d.getId());
            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }
//         结果是既有菜品又有菜品口味数据.



        return dishVOList;
    }
}
